// 供方预付款结算
<template>
    <div class="e-table billList">
        <com-top-step :stepInfo="topStep" />
        <div class="top">
            <div class="left">
                <div class="left-btn">
                    <el-button
                        size="mini"
                        @click="goAdd"
                        style="background: #7aa818; color: #fff"
                        v-permission="['edit']"
                        >新增</el-button
                    >
                </div>
            </div>
            <div class="search_box">
                <el-input
                        placeholder=""
                        v-model="query.keyword"
                        size="small"
                        @blur='getData'
                        @keyup.enter.native="getData"
                    ><img
                        src="@/assets/search.png"
                        slot="suffix"
                        @click="getData"
                /></el-input>
                <div class="adverse" @click="onAdvanceSearch">
                    <el-button type="primary" size="small">高级搜索</el-button>
                </div>
            </div>
        </div>

        <el-table
            border
            ref="table"
            :data="tableData"
            style="width: 100%"
            class="table"
            highlight-current-row
            height="0"
            v-sortTable="{ 'tableData': tableData, '_this': this, 'ref': 'table' }"
        >
            <el-table-column
                prop="billNo"
                label="结算编号"
                min-width="300"
                fixed
                show-overflow-tooltip
            >
                <template slot-scope="{ row }">
                    <span style="color: blue; cursor: pointer" @click="onEdit(row)">{{
                        row.billNo || '无'
                    }}</span>
                </template>
            </el-table-column>
            <el-table-column prop="settlementDate" label="结算日期" width="150" fixed>
            </el-table-column>

            <el-table-column
                prop="settlementUnitName"
                label="结算单位"
                min-width="200"
            >
            </el-table-column>
            <el-table-column prop="contractName" label="合同名称" width="200">
            </el-table-column>
            <el-table-column
                prop="contractTypeName"
                label="合同类型"
                width="150"
            >
            </el-table-column>
            <el-table-column prop="amount" label="预付金额(元)" align="right" width="150">
            </el-table-column>
            <el-table-column
                prop="writeOffAmount"
                label="冲预付金额(元)"
                width="150"
                align="right"
            >
            </el-table-column>
            <el-table-column label="评价" width="100" prop="evaluationLevel">
                <template slot-scope="{ row }">
                    <span
                        @click="dynamicEvaluation(row)"
                        style="color: blue; cursor: pointer"
                    >
                        {{
                            row.evaluationLevel
                                ? row.evaluationLevel
                                : '动态评价'
                        }}
                    </span>
                </template>
            </el-table-column>
            <el-table-column prop="state" label="状态" width="150">
               <template slot-scope="scope">
                    <ToolHistory
                        v-if="scope.row.state === 1"
                        :key="scope.$index"
                        url="/turnover/audit/getHistoryList"
                        :billId="scope.row.billId"
                        :billType="1018"
                    >
                        <span v-status="scope.row.state">
                            {{ showState(scope.row.state) }}
                        </span>
                    </ToolHistory>
                    <span v-status="scope.row.state" v-else>{{showState(scope.row.state)}}</span>
                </template>
            </el-table-column>
            <el-table-column prop="redBillState" label="红单状态" width="150">
                <template v-slot="{ row }">
                    <span
                        style="color: blue; cursor: pointer"
                        @click="redClick(row)"
                        >{{ row.redState }}</span
                    >
                </template>
            </el-table-column>
            <el-table-column prop="orgName" label="机构" min-width="200" show-overflow-tooltip align="left">
            </el-table-column>
        </el-table>
        <Pagination
            :total="total"
            :pageSize.sync="query.limit"
            :currentPage.sync="query.page"
            @currentChange="getData"
            @sizeChange="getData"
            class="bottomSetting"
        />
    </div>
</template>

<script>
import ComTopStep from '@/components/topstep/topstep.vue'
import Pagination from '@/components/pagination/pagination'
import request from '@/api/materialSettlement/supplierPayment'
import publicRequest from '@/api/materialSettlement/public.js'
import { vueDom, debounce, openAppClient, advanceSearch, showLoading, hideLoading } from '@/utils/common'
import { mapMutations } from 'vuex'
export default {
    components: {
        Pagination,
        ComTopStep
    },
    computed: {
        // //获取状态
        // statusLable () {
        //     return param => {
        //         const state = this.$store.state.settlementManagement.ysState
        //         const obj = state.find(x => x.value === Number(param))
        //         return obj.label
        //     }
        // },

    },
    data () {
        return {
            topStep: [
                {
                    description: '物资合同'
                },
                {
                    description: '供方预付款结算'
                }
            ],
            query: {
                beginPlanDate: '',
                contractType: [],
                endPlanDate: '',
                keyword: '',
                limit: 20,
                orgId: '',
                page: 1,
                settlementUnitId: '',
                states: []
            },
            tableData: [], //表格
            //动态评价参数
            evaluationBasis: {
                contractName: '', //合同名称
                contractNo: '', //合同编号
                outerId: '', //外包方id
                outerName: '', //外包方名称
                billId: '', //调用单据id
                billType: '1018', //	调用单据编号
                evaluatePeople: '', //评价人
                evaluatePeopleId: '', //评价人id
                orgId: '', //评价机构id
                orgName: '', //	评价机构名称
                settleNo: '', //结算编号
                useType: 4
            },
            total: 1,
            tableHeight: 0,
            auditParams: {
                billId: '',
                billType: 1018,
                freeNextUnitIndex: null,
                freeNextUser: '',
                isPass: true,
                isShareInAudit: true,
                opinion: '',
                orgId: '',
                orgName: '',
                userId: '',
                workId: ''
            },
        }
    },
    created () {
        let data = JSON.parse(localStorage.getItem('vuex'))
        this.evaluationBasis.orgId = data.userInfo.orgInfo.orgId
        this.evaluationBasis.orgName = data.userInfo.orgInfo.orgName
        this.evaluationBasis.evaluatePeople = data.userInfo.userName
        this.evaluationBasis.evaluatePeopleId = data.userInfo.userId
        this.getData()
    },
    mounted () {
        // // 获取屏幕宽高
        // this.getScreenInfo()
        // this.winEvent.onResize = throttle(this.getScreenInfo, 500)
        // window.addEventListener('resize', this.winEvent.onResize)

        // table动态高度
        vueDom(
            () => {
                this.tableHeight = document.body.clientHeight - 240
            },
            () => {
                return $('table').length !== 0
            }
        )
        this.fn = () => {
            this.tableHeight = document.body.clientHeight - 240
        }
        window.onresize = debounce(this.fn)
    },
    // beforeDestroy () {
    //     window.removeEventListener('resize', this.winEvent.onResize)
    // },

    methods: {
        ...mapMutations(['setEvaluation']),

        async getData () {
            try{
                showLoading()
                const res = await request.getpaymentList(this.query)
                let data = res.list || []
                data.forEach(item => {
                    switch (item.redBillState) {
                    // 用redState来表示值的目的是:不改变原有的redBillState的数字值,否则在跳转页面时不方便做判断
                    case -1:
                        item.redState = ''
                        break
                    case 0:
                        item.redState = '冲销中'
                        break
                    case 1:
                        item.redState = '冲销中'
                        break
                    case 2:
                        item.redState = '已冲销'
                        break
                    case 3:
                        item.redState = '红单'
                        break
                    }
                })
                this.tableData = data
                this.total = res.totalCount
            }finally{
                hideLoading()
            }
        },
        goAdd () {
            //点击新增按钮
            this.$router.push('/supplierPaymentAdd')
        },
        onEdit (row) {
            if (row.state === -1 || row.state === 2 || row.state === 5 || !this.permission(['edit'])) {
                this.$router.push({
                    path: '/supplierPaymentView',
                    query: { billid: row.billId }
                })
            } else {
                this.auditParams.billId = row.billId
                publicRequest.getCurrentUnitInfo(this.auditParams).then(res => {
                    if (res.canModify == true) {
                        this.$router.push({
                            path: '/supplierPaymentEdit',
                            query: { billid: row.billId }
                        })
                    } else {
                        this.$router.push({
                            path: '/supplierPaymentView',
                            query: { billid: row.billId }
                        })
                    }
                })
            }
        },
        onAdvanceSearch () {
            advanceSearch('/supplierPaymentSearch').then(res => {
                this.query.beginPlanDate = res.beginPlanDate
                this.query.contractType = res.contractType
                this.query.endPlanDate = res.endPlanDate
                this.query.orgId = res.orgId
                this.query.states = res.states
                this.query.settlementUnitId = res.settlementUnitId
                this.getData()
            })
        },
        //公共方法 ↓
        getScreenInfo () {
            let screenWidth = document.documentElement.clientWidth || document.body.clientWidth
            let screenHeight = document.documentElement.clientHeight || document.body.clientHeight
            this.screenWidth = screenWidth
            this.screenHeight = screenHeight
        },

        // 动态评价
        dynamicEvaluation (row) {
            this.evaluationBasis.contractName = row.contractName
            this.evaluationBasis.contractNo = row.contractBillNo
            this.evaluationBasis.outerId = row.settlementUnitId
            this.evaluationBasis.outerName = row.settlementUnitName
            this.evaluationBasis.billId = row.billId
            this.evaluationBasis.settleNo = row.billNo
            this.setEvaluation(this.evaluationBasis)
            openAppClient(
                '/dynamicEvaluation',
                '动态评价',
                () => {},
                null,
                null,
                'handleAdd'
            )
        },
        // 红单
        redClick (row) {
            this.auditParams.billId = row.billId
            if (row.redBillState == 3) {
                // 如果该单据就是红单,则点击时进入源单详情页
                this.$router.push({
                    path: '/supplierPaymentView',
                    query: { billid: row.sourceId }
                })
            } else {
                // 如果点击的该单据不是红单,则点击超链接进入红单
                // 如果进入红单,还需判断进入该红单的编辑页还是详情页
                if (row.redBillState == 0) {
                    this.$router.push({
                        path: '/supplierPaymentEdit',
                        query: { billid: row.redBillId }
                    })
                } else if (row.redBillState == 1 || row.redBillState == 2) {
                    this.$router.push({
                        path: '/supplierPaymentView',
                        query: { billid: row.redBillId }
                    })
                }
            }
        },
        showState (state) {
            const _state = this.$store.state.settlementManagement.ysState
            const obj = _state.find(x => x.value === Number(state))
            return obj.label
        }
    }
}
</script>

<style lang="scss" scoped>
::v-deep .el-table__fixed{
    height: 98.5% !important;
    bottom: 17px !important;
}
::v-deep .el-table__fixed-body-wrapper {
    height: 100% !important;
}
</style>
